﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dominio;
using FluentNHibernate.Mapping;
using FluentNHibernate;

namespace Infra.NHibernateFramework {
    public class CategoriaBaseMap : ClassMap<CategoriaBase> {
        public CategoriaBaseMap() {
            Table("tbl_Categoria");

            Id(x => x.Id, "IdCategoria");

            Map(x => x.Nome)
                .Not.Nullable()
                .Length(100);

            DiscriminateSubClassesOnColumn<int>("TipoCategoria", (int)-1);
        }
    }

    public class CategoriaMap : SubclassMap<Categoria> {
        public CategoriaMap() {

            References(x => x.Usuario, "IdUsuario")
                .Nullable();

            HasMany(x => x.SubCategorias)//<SubCategoria>(Reveal.Member<Categoria>("SubCategorias"))
                .KeyColumn("IdCategoriaMae")
                .Inverse()
                .Fetch.Join()
                .Cascade.AllDeleteOrphan()
                .Not.LazyLoad()
                .AsBag();

            DiscriminatorValue((int)TipoCategoria.CATEGORIA);
        }
    }

    public class SubCategoriaMap : SubclassMap<SubCategoria>{
        public SubCategoriaMap() {
            DiscriminatorValue((int)TipoCategoria.SUB);

            References(x => x.Categoria, "IdCategoriaMae")                
                .Nullable();
        }
    }
}
